package com.simpleaccount.User;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.simpleaccount.Login.LoginActivity;
import com.simpleaccount.R;
import com.simpleaccount.Toast.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2017/7/19.
 */

public class ChangepswActivity extends Activity {

    @Bind(R.id.et_oldpassword)
    EditText etOldpassword;
    @Bind(R.id.et_newpassword)
    EditText etNewpassword;
    @Bind(R.id.et_cfm_password)
    EditText etCfmPassword;
    @Bind(R.id.bn_submit)
    Button bnSubmit;
    @Bind(R.id.tv_changepsw)
    TextView tvChangepsw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {//2
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_changepassword);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {//initView
        //对btn进行实例化
    }//initView

    @Override
    protected void onPause() {
        super.onPause();
        ToastUtils.cancel();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ToastUtils.cancel();
    }

    @Override
    protected void onStop() {
        super.onStop();
        ToastUtils.cancel();
    }

    @OnClick(R.id.bn_submit)
    public void onViewClicked() {
        String oldPsw = etOldpassword.getText().toString();
        String newPsw = etNewpassword.getText().toString();
        String cfmPsw = etCfmPassword.getText().toString();
        if (!newPsw.equals(cfmPsw)) {
            ToastUtils.toast(ChangepswActivity.this, "两次输入密码不一致", "warning");
            return;
        }
        BmobUser bn1 = new BmobUser();
        BmobUser bn2 = new BmobUser();
        bn1.setPassword(oldPsw);
        bn2.setPassword(newPsw);
        BmobUser.updateCurrentUserPassword(oldPsw, newPsw, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    BmobUser.logOut();   //清除缓存用户对象
                    ToastUtils.toast(ChangepswActivity.this, "修改成功，请重新登录", "success");
                    Intent intent_tologin = new Intent(ChangepswActivity.this, LoginActivity.class);
                    startActivity(intent_tologin);
                } else {
                    if (e.getErrorCode() == 9016 || e.getErrorCode() == 9010) {
                        ToastUtils.toast(ChangepswActivity.this, "网络错误，请检查您的网络", "err");
                    } else if (e.getErrorCode() == 210) {
                        ToastUtils.toast(ChangepswActivity.this, "原密码不正确", "warning");
                    }
                }
            }

        });
    }
}
